package com.link2room.bumblebee.util;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ExceptionUtil;
import com.link2room.common.util.LogUtil;
import com.link2room.common.util.SpringUtil;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author sqj88
 * @date 2018/1/18 17:03
 */
public class SysNoUtil {
    static Map<String,ReentrantLock> map=new HashMap<>();

    private static ICommonService commonService;

    public static String getNo(String lhotelGroupCode,String noType) throws Exception {
        String key=getSysNoKey(lhotelGroupCode,noType);
        ReentrantLock reentrantLock = null;
        try {
            reentrantLock=Optional.ofNullable(map.get(key)).orElse( new ReentrantLock());
            reentrantLock.lock();
            List params=Stream.of(lhotelGroupCode,noType).collect(Collectors.toList());
            commonService=Optional.ofNullable(commonService).orElse((ICommonService)SpringUtil.getBean("commonServiceSupport"));
            return commonService.findOneFieldByProcedure("create_No",params);
        } finally {
            reentrantLock.unlock();
        }
    }

    public static String getSysNoKey(String lhotelGroupCode,String noType){
        return lhotelGroupCode+ DefaultValue.CUT+noType;
    }
}
